package processor

import (
	"chat-room/common"
	"chat-room/server/global"
	"encoding/json"
	"fmt"
	"net"
)

type Message struct {
	Conn net.Conn
}

func (that *Message) MassMessage(msgStr string){
	//传递消息结构体
	val,_ :=global.AllLoginMap.Load(that.Conn)
	messageEx := &common.Message{
		Type: common.MassMessageResponseMsgType,
		Data: val.(string)+"："+msgStr,
	}

	messageExByte, _ := json.Marshal(messageEx)

	functionsEx := common.Functions{}
	global.AllLoginMap.Range(func(key, value interface{}) bool {
		if key != that.Conn {
			err := functionsEx.SendMsg(key.(net.Conn), messageExByte)
			if err != nil {
				fmt.Printf("functionsEx.SendMsg:%v\n",err)
			}
		}
		return true
	})

	messageEx.Type = common.MassMessageSuccessResponseMsgType
	messageEx.Data = "";
	messageExByte, _ = json.Marshal(messageEx)
	err := functionsEx.SendMsg(that.Conn, messageExByte)
	if err != nil {
		fmt.Printf("functionsEx.SendMsg:%v\n",err)
	}
}